/**
 * 登录模块
 */
$(document).ready(function(){
	Login.init();
});
//主要是验证框架
var Login = function () {
	return {
	    //main function to initiate the module
	    init: function () {
	       $('.login-form').validate({
	            errorElement: 'div', //default input error message container
	            errorClass: 'text-danger', // default input error message class
	            focusInvalid: false, // do not focus the last invalid input
	            rules: {
	                username: {
	                    required: true,
	                },
	                password: {
	                    required: true,
	                    minlength:8,
	                    maxlength:16
	                },
	            },
	            messages: {
	                username: {
	                    required: "用户名不能为空.",
	                },
	                password: {
	                    required: "密码不能为空.",
	                    minlength: "密码长度不能小于8位",
	                    maxlength: "密码长度不能超过16位"	                    
	                }
	            },
	            invalidHandler: function (event, validator) { //display error alert on form submit   
	               // $('.alert-error', $('.login-form')).show();
	            	console.log(event);
	            },
	            highlight: function (element) { // hightlight error inputs
	                //$(element).closest('.control-group').addClass('error'); // set error class to the control group
	            },
	            success: function (label) {
	            	//console.log("is success");
	                //label.closest('.control-group').removeClass('error');
	                //label.remove();
	            },
	            errorPlacement: function (error, element) {
	            	//error.prepend("<i class='icon-remove bigger-110 red'></i>");
	                error.insertAfter(element.closest('.input-icon'));
	            	//error.insertAfter("li").insertAfter("i").addClass("icon-remove bigger-110 red");
	            	//console.log(error);
	            },
	            submitHandler: function (form) {
	            	var data = {name:$("#username").val(),password:$("#password").val()};
	            	iManager.login({
						data:data,
						callback:LoginHandle
					})
	            	// var result = LoginSend.send(data,{
	            	// 	callback:LoginHandle
	            	// });
	            }
	        });
	    }
	};
}();
//服务端数据返回处理
var LoginHandle = function(obj){
	console.log(obj);
	//异常返回
	if(obj.code != "1"){
		var errorDescript = errorCode.getErrorDescript({
			code:obj.code
		});
		
		$("#warning-login").attr("class","alert alert-danger");
		$("#warning-login").find("span").text("错误！" + errorDescript);
		window.setTimeout(function(){
			$("#warning-login").attr("class","alert alert-block hide");
		},3000);
	}else{
		var token = obj.tokenCode;
		sessionStorage.setItem("accountNickName",obj.nickname);
//		sessionStorage.setItem("tokenCode",obj.tokenCode);
		sessionStorage.setItem("accountAvatar",obj.avatar);
		
		location.href = "main.html";
	}
}


//静态资源根据邮箱跳转
var errorCode = (function(){
	var hash={   
		'1001':'用户名或密码为空!',
		'1002':'用户名或密码错误!',
		'1003':'用户名或密码错误!',
		'-1000':'系统错误!',   
		'0':'处理失败',   
	};
	return{
		getErrorDescript:function(option){
			console.log(option)
    		for (var j in hash){   
    			option.descript=hash[option.code];
    		}
    		return option.descript
		}
	}
}());



